/*
 * Copyright (C) 2008-2009 the original author or authors
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *         http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.code.liquidform.internal;

import java.lang.reflect.Array;

import com.google.code.liquidform.LiquidForm;

/**
 * Utility class that factors out common behavior with objects.
 * 
 */
public class ObjectUtils {

	/**
	 * Returns an array of objects that contains the <tt>first</tt> item, merged
	 * with any other optional item passed in the <tt>more</tt> varargs. This
	 * funny method signature is useful when we want to make sure that the user
	 * provides at least one item, such as in
	 * {@link LiquidForm#select(Object, Object...)}
	 * 
	 */
	@SuppressWarnings("unchecked")
	public static <T> T[] merge(Class<T> clazz, T first, T... more) {
		int morel = 0;
		if (more != null) {
			morel = more.length;
		}

		T[] merged = (T[]) Array.newInstance(clazz, 1 + morel);
		merged[0] = first;
		System.arraycopy(more, 0, merged, 1, morel);

		return merged;
	}

}
